########################################################################
#
# Configuration variables, please modify
#

# build in debug mode
#DEBUG=yes

# Whether to build a python module of ephi using swig.
#PYTHON=yes

# What type of floating point numbers to use (default: double)
# float, double, longdouble, error, gmp
#   float - unstable, longdouble (quad precision) - mostly works, gmp - mostly works
#PRECTYPE=longdouble

# when using gmp/error precision type, size of the floating point type
GMP_MPF_BITS=256

# Build with png mage format support
#PNG=yes

# Build with support for threads, use this many threads
# You should enter as many CPU-s/cores you have
#THREADS_CPUCOUNT=4

#
########################################################################
#
# no need to modify from this point on..
#

APPS = test1 test2 test3 test4 test5 test6 polywell polywell_bfield bfield_animate \
	wallpapers bfield_lines polywell_efield test7 polywell_pcp test8 test9 test10 \
	polywell_rect_vs_round test11 test12 polywell_slice test13 polywell_confine \
	polywell_force polywell_pierce mapgen coilgen itest ipoly rktest polyp test15 \
	test14 test16 ivis sectors otree wb1 test17 chargedist test18 test19 wb2 test20 \
	test21 test22 test23 test24 test25 wb3 #polywell_double tt tt3 #tt2 #tt # tt

ifeq ($(PNG),yes)
  LDFLAGS += -lpng
  CXXFLAGS += -DENABLE_PNG
endif

ifeq ($(PYTHON),yes)
  APPS += _ephi.so
  CXXFLAGS += -fpic
endif

ifeq ($(PRECTYPE),float)
  CXXFLAGS += -DUSE_FLOAT
  SWIGFLAGS += -DUSE_FLOAT
endif

ifeq ($(PRECTYPE),longdouble)
  CXXFLAGS += -DUSE_DOUBLEDOUBLE
  SWIGFLAGS += -DUSE_DOUBLEDOUBLE
endif

ifeq ($(PRECTYPE),error)
  CXXFLAGS += -DUSE_ERRORPREC
  SWIGFLAGS += -DUSE_ERRORPREC
  CXXFLAGS += -DGMP_MPF_BITS=$(GMP_MPF_BITS)
  LDFLAGS += -lgmp
endif

ifeq ($(PRECTYPE),gmp)
  CXXFLAGS += -DUSE_GMP
  CXXFLAGS += -DGMP_MPF_BITS=$(GMP_MPF_BITS)
  SWIGFLAGS += -DUSE_GMP
  LDFLAGS += -lgmp
endif

ifneq ($(THREADS_CPUCOUNT),)
  LDFLAGS += -lpthread
  CXXFLAGS += -DTHREADS_CPUCOUNT=$(THREADS_CPUCOUNT)
endif

all: libephi.a $(APPS)

OBJS = statics.o math3d.o screen.o scene.o dynamics.o polywell_conf.o config.o ellipse.o ephi.o threading.o coildata.o potential.o matrix.o octree.o polywell_sectors.o pmap.o coil.o plasma.o

CXXFLAGS += -Wall

#CXXFLAGS += -m32
#LDFLAGS += -m32

#LDFLAGS += -static

ifeq ($(DEBUG),yes)
  CXXFLAGS += -g -O0
  LDFLAGS += -g -O0
else
  CXXFLAGS += -O2
endif

ifeq ($(USE_FLOAT),yes)
  CXXFLAGS += -DUSE_FLOAT
endif

libephi.a: $(OBJS)
	ar rcs libephi.a $(OBJS)

LDFLAGS += -L. -lephi

polywell: polywell.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test1: test1.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test2: test2.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test3: test3.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test4: test4.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test5: test5.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test6: test6.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polywell_bfield: polywell_bfield.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polywell_efield: polywell_efield.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

bfield_animate: bfield_animate.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

wallpapers: wallpapers.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

bfield_lines: bfield_lines.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test7: test7.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polywell_pcp: polywell_pcp.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test8: test8.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test9: test9.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test10: test10.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polywell_rect_vs_round: polywell_rect_vs_round.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test11: test11.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test12: test12.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test13: test13.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

otree: otree.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

wb1: wb1.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polywell_slice: polywell_slice.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polywell_pierce: polywell_pierce.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polywell_confine: polywell_confine.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polywell_force: polywell_force.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

mapgen: mapgen.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

coilgen: coilgen.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

itest: itest.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

ipoly: ipoly.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

rktest: rktest.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polyp: polyp.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polyp2: polyp2.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test15: test15.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test14: test14.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test16: test16.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

ivis: ivis.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

sectors: sectors.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test17: test17.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

chargedist: chargedist.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test18: test18.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test19: test19.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

wb2: wb2.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test20: test20.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test21: test21.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test22: test22.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test23: test23.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test24: test24.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

test25: test25.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

wb3: wb3.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

tt: tt.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

tt3: tt3.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

tt2: tt2.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

polywell_double: polywell_double.o libephi.a
	$(CXX) $^ -o $@ $(LDFLAGS)

%.o: %.asm
	nasm -f elf $<

clean:
	rm -f *.o $(APPS) test1.bmp test4.bmp test5.bmp test6*.bmp test7*.bmp polywell*.bmp polywell.inc polywell.png solenoid_lines.bmp polywell_cube.csv ephi_wrap.cxx ephi_wrap.o ephi.py *.pyc _ephi.so libephi.a *.csv rectpoly_*.bmp itest*.bmp test16_* test13_*.bmp test14_*.bmp ivis_*.bmp otree_*.bmp wb1.cgen wb1_* test19_*

distclean: clean
	rm -f .depend

depend:
	g++ -MM *.cpp $(CXXFLAGS) > .depend

_ephi.so: ephi_wrap.o libephi.a
	$(CXX) -shared $(LDFLAGS) ephi_wrap.o libephi.a -o _ephi.so

ephi_wrap.o: ephi.swig libephi.a
	swig -python -c++ $(SWIGFLAGS) ephi.swig
	$(CXX) -I/usr/include/python -c $(CXXFLAGS) ephi_wrap.cxx

-include .depend
